home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0051_Line Drawing.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  2KB  |  65 lines

  1. {
  2. > I used something like this:
  3. > for x := 1 to 100 do
  4. > begin
  5. >      y := slope*x;
  6. >      putpixel(x,y);
  7. > end;
  8.  
  9. the slope method is a close cousin to bubble-sort an algorithm to use if
  10. you can't be bothered to use a more efficient one for the job.
  11.  
  12. here's one. that only uses addition and subtraction in it's loop.
  13. (FWIW it's based on the commutativity of multiplication.)
  14.  
  15. I think It's got some fancy name which I forget, this code is 100% my
  16. own (freeware) and reasonably well tested.
  17. }
  18.  
  19.   procedure myline(x1,y1,x2,y2,color:integer);
  20.  
  21.     {Freeware: my bugs - your problem , 29 dec 1993 J.Betts,
  22.      PASCAL echo Fidonet.     please keep this notice intact}
  23.  
  24.   function sign(x:integer):integer; {like sgn(x) in basic}
  25.   begin if x<0 then sign:=-1 else if x>0 then sign:=1 else sign:=0 end;
  26.   var
  27.     x,y,count,xs,ys,xm,ym:integer;
  28.   begin
  29.     x:=x1;y:=y1;
  30.  
  31.     xs:=x2-x1;    ys:=y2-y1;
  32.  
  33.     xm:=sign(xs); ym:=sign(ys);
  34.     xs:=abs(xs);  ys:=abs(ys);
  35.  
  36.     putpixel(x,y,color);
  37.  
  38.   if xs > ys
  39.     then begin {flat line <45 deg}
  40.       count:=-(xs div 2);
  41.       while (x <> x2 ) do begin
  42.         count:=count+ys;
  43.         x:=x+xm;
  44.         if count>0 then begin
  45.           y:=y+ym;
  46.           count:=count-xs;
  47.           end;
  48.         putpixel(x,y,color);
  49.         end;
  50.       end
  51.     else begin {steep line >=45 deg}
  52.       count:=-(ys div 2);
  53.       while (y <> y2 ) do begin
  54.         count:=count+xs;
  55.         y:=y+ym;
  56.         if count>0 then begin
  57.           x:=x+xm;
  58.           count:=count-ys;
  59.           end;
  60.         putpixel(x,y,color);
  61.         end;
  62.       end;
  63.   end;
  64.  
  65.